package org.jeecg.modules.zonglzp.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.backstageModule.service.BackCustService;
import org.jeecg.modules.gwcjava.jiekou.entity.MlccUserInfo;
import org.jeecg.modules.gwcjava.jiekou.service.MlccUserInfoService;
import org.jeecg.modules.zonglzp.entity.CompanyEntity;
import org.jeecg.modules.zonglzp.entity.Customer;
import org.jeecg.modules.zonglzp.entity.User;
import org.jeecg.modules.zonglzp.entity.store;
import org.jeecg.modules.zonglzp.mapper.CustomerMapper;
import org.jeecg.modules.zonglzp.service.CompanyService;
import org.jeecg.modules.zonglzp.service.orderAfterService;
import org.jeecg.modules.zonglzp.util.WordUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

//import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

@RestController
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private CompanyService service;

    @Autowired
    private MlccUserInfoService userservice;

    @Autowired
    CustomerMapper customerMapper;


    @Autowired
    orderAfterService orderAfterService;


    @Autowired
    BackCustService backCustService;
    /*
    通过用户token查询该用户的公司
     */
    @PostMapping("/querycompanyBytoken")
    public JSONObject querycompanyBytoken(
            @RequestBody String token) {
        token = JSONObject.parseObject(token).getString("token");
        JSONObject obj = new JSONObject();
        //通过用户token获取用户id
        String userid = service.getUseridbytoken(token);
        if (userid == null) {
            obj.put("code", 1);
            obj.put("data", "token失效或存户不存在");
            return obj;
        }
        CompanyEntity companyEntity = service.getCompanybyuserId(userid);
        if (companyEntity == null) {
            obj.put("code", 1);
            obj.put("data", "未查到公司信息");
        } else {
            obj.put("code", 0);
            obj.put("data", companyEntity);
        }
        return obj;
    }


    /*
      通过公司id查询门店
     */
    @GetMapping("/querystoreBycompanyId")
    public JSONObject querystoreBycompanyId(
            @RequestParam(name = "companyId") String companyId,
            @RequestParam(name = "token") String token,
            @RequestParam(name = "pageSize", defaultValue = "100") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo
    ) {
        //通过token查询权限代码
        String orgCode=orderAfterService.getuserorgCode(token);
        JSONObject obj = new JSONObject();
        //通过用户token获取用户id
        String userid = service.getUseridbytoken(token);
        if (userid == null) {
            obj.put("code", 1);
            obj.put("data", "token失效或存户不存在");
            return obj;
        }
        Page<store> page = new Page<store>(pageNo, pageSize);
        IPage<store> pageList = service.querystoreBycompanyId(companyId, page,orgCode);
        if (pageList.getRecords().size() > 0) {
            obj.put("data", pageList);
            obj.put("code", 0);
        } else {
            obj.put("data", "暂无门店信息");
            obj.put("code", 1);
        }
        return obj;
    }


    /**
     * 通过门店id查询门店的所有地址-->改为根据公司编码
     * @param pageSize
     * @param pageNo
     * @return
     */
    @GetMapping("/getaddressbystoreId")
    public JSONObject getaddressbystoreId(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "token") String token){
        JSONObject obj = new JSONObject();
        MlccUserInfo userInfo = userservice.queryUserinfoByusername(token, 3);
        if (userInfo == null) {
            obj.put("code", 4);
            obj.put("msg", "token已失效，请重新登录");
            return obj;
        }
        Page<store> page = new Page<store>(pageNo, pageSize);
        IPage<store> pageList = service.getaddressbystoreId(page,userInfo.getCompanyCode());
        obj.put("code", 0);
        obj.put("msg", "查询成功");
        obj.put("data", pageList);
        return obj;
    }


    /*
    添加门店的地址
     */
    @PostMapping("/addstoreaddress")
    public JSONObject addstoreaddress(@RequestBody store store){
        JSONObject obj = new JSONObject();
        int count=service.addstoreaddress(store);
        if (count > 0) {
            obj.put("code", 0);
            obj.put("data", "新增成功");
        } else {
            obj.put("code", 1);
            obj.put("data", "新增失败");
        }
        return obj;
    }

    /*
编辑门店的地址
 */
    @PostMapping("/updatestoreaddress")
    public JSONObject updatestoreaddress(@RequestBody store store){
        JSONObject obj = new JSONObject();
        int count=service.updatestoreaddress(store);
        if (count > 0) {
            obj.put("code", 0);
            obj.put("data", "编辑成功");
        } else {
            obj.put("code", 1);
            obj.put("data", "编辑失败");
        }
        return obj;
    }

    /**
     *删除门店的地址
     * @param
     * @return
     */
    @GetMapping("/deletestoreaddress")
    public JSONObject deletestoreaddress(
            @RequestParam(name = "id") int id
    ){
        JSONObject obj = new JSONObject();
      int count=service.deletestoreaddress(id);
        if (count > 0) {
            obj.put("code", 0);
            obj.put("data", "删除成功");
        } else {
            obj.put("code", 1);
            obj.put("data", "删除失败");
        }
        return obj;
    }



    /*
    通过公司编码查询公司下面所有员工
     */
    @GetMapping("/getyuangonglistBycompanyId")
    public JSONObject getyuangonglistBycompanyId(
            @RequestParam(name = "token") String token,
            @RequestParam(name = "companyOrgcode") String companyId,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo
    ) {
        JSONObject obj = new JSONObject();

        //通过用户token获取用户id
        String userid = service.getUseridbytoken(token);
        if (userid == null) {
            obj.put("code", 1);
            obj.put("data", "token失效或存户不存在");
            return obj;
        }


        //通过token获取公司编码

        companyId=service.getcompanycodebytoken(token);
        Page<User> page = new Page<User>(pageNo, pageSize);
        IPage<User> pageList = service.getyuangonglistBycompanyId(companyId, page);

        if (pageList.getRecords().size() > 0) {
            obj.put("code", 0);
            obj.put("data", pageList);
        } else {

            obj.put("code", 1);
            obj.put("data", "暂无员工");
        }

        return obj;
    }

    /*
    新增门店
     */
    @PostMapping("/addstore")
    public JSONObject addstore(
            @RequestBody store store) {
        JSONObject obj = new JSONObject();


        try {

            //判断前端传来的信息是否完整
            if (store.getStoreName() == null || "".equals(store.getStoreName())) {
                obj.put("code", 1);
                obj.put("data", "门店名未输入，请输入后再提交");
                return obj;
            }


            //判断门店名称是否已存在
//            String storeName = service.querystoreName(store.getStoreName());
//            if (storeName != null) {
//                obj.put("code", 1);
//                obj.put("data", "店名已存在，请重新命名");
//                return obj;
//            }



            //先随机生成一个工号
           // Long startTs = System.currentTimeMillis(); // 当前时间戳
            //8位随机数
            //int random = new Random().nextInt(100) + 1;//为变量赋随机值1-99999999;
            WordUtils util = new WordUtils();
            String wenzi = util.getUpperCase(store.getStoreName(), false);
            String storecode = store.getCompanyCode() +"-"+ wenzi;
            store.setStoreCode(storecode);

            //判断门店编号是否已存在
            String storeCode = service.querystoreCode(store.getStoreCode());
            if (storeCode != null) {
                obj.put("code", 1);
                obj.put("data", "店名已存在，请重新命名");
                return obj;
            }







            int count = service.addstore(store);

                //通过门店编码查询门店id
        //    int storeId=service.querystoreIdbycode(store.getStoreCode());


//            User user=new User();
//
//
//            user.setPhone(store.getDianzhangphone());
//            user.setRealName(store.getDianzhangname());
//            user.setStaffNumber(store.getDianzhangworknumber());
//            user.setStoreId(String.valueOf(storeId));
//            //先随机生成一个工号
//            Long startTs = System.currentTimeMillis(); // 当前时间戳
////            //8位随机数
//            int random = new Random().nextInt(100) + 1;//为变量赋随机值1-99999999;
//            String worknumber = random + (startTs.toString());
//            user.setWorkNumber(worknumber);
//            user.setPassWord(String.valueOf(System.currentTimeMillis()));
//            //设置密码眼
//            String salt = oConvertUtils.randomGen(8);
//            user.setSalt(salt);
//            String passwordEncode = PasswordUtil.encrypt(user.getWorkNumber(), user.getPassWord(), salt);
////            //设置密码
//            user.setPassWord(passwordEncode);
//
//            //设置token
//            //  String token2 = JwtUtil.sign(user.getUserName(), user.getPassWord());
//            //先把时间戳当token用吧
//            user.setToken(startTs.toString());
//            user.setCreateTime(new Date());
//            user.setMonthLines("0");
//            user.setUserName(user.getPhone());
//            user.setIdentity(2);
//            user.setCompanyOrgcode(store.getCompanyCode());
//            int count2 = service.addstaff(user);
//
//
//            //通过账号查询这个user的id
//            int userId = backCustService.getuserIdbyusexrname(user.getUserName());
//
//            //将门店id赋给这个用户
//            backCustService.updateuserstoreId(userId,storeId);
//
//
//            //通过公司编码去查询上级的权限编码
//            String upquanxian=service.getupquanxian(store.getCompanyCode());
//
//
//            //权限操作，加一个权限编码
//            service.updatedianzhangOrgCode(userId,upquanxian);




            if (count > 0) {
                obj.put("code", 0);
                obj.put("data", "新增成功");
            } else {
                obj.put("code", 1);
                obj.put("data", "新增失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            obj.put("code", 1);
            obj.put("data", "新增失败请重试");
        }
        return obj;
    }

    /*
     删除门店
     */
    @GetMapping("/deletestore")
    public JSONObject deletestore(
            int storeid,
            @RequestParam(name = "token") String token
    ) {
        JSONObject obj = new JSONObject();
        //通过用户token获取用户id
        String userid = service.getUseridbytoken(token);
        if (userid == null) {
            obj.put("code", 1);
            obj.put("data", "token失效或存户不存在");
            return obj;
        }

        //删除门店前先检查该门店下是否有员工
         List<User> userList=service.getuserListbyStoreid(storeid);
         if(userList!=null&&userList.size()>0){
             obj.put("code", 1);
             obj.put("data", "该门店下还有员工,请先删除员工");
             return obj;
         }

        int count = service.deletestore(storeid);
        if (count > 0) {
            obj.put("code", 0);
            obj.put("data", "删除成功");
        } else {
            obj.put("code", 1);
            obj.put("data", "删除失败");
        }
        return obj;
    }

    /*
    编辑客户
     */
    @PostMapping("/updatecustomer")
    public JSONObject updatecustomer(
            @RequestBody Customer customer
    ){
        JSONObject obj = new JSONObject();
        //查询之前是否有这个名字的客户

        if (customer.getCustomerName() == null || "".equals(customer.getCustomerName())) {
            obj.put("code", 1);
            obj.put("data", "未输入名字，请输入");
            return obj;
        }
        int count = service.updatecustomer(customer);
        if (count > 0) {
            obj.put("code", 0);
            obj.put("data", "修改成功");
        } else {
            obj.put("code", 1);
            obj.put("data", "修改失败");
        }
        return obj;
    }

    /*
    新增客户
     */
    @PostMapping("/addcustomer")
    public JSONObject addcustomer(
            @RequestBody Customer customer) {

        JSONObject obj = new JSONObject();
        String userid = service.getUseridbytoken(customer.getToken());
        if (userid == null) {
            obj.put("code", 1);
            obj.put("data", "token失效或存户不存在");
            return obj;
        }
        //查询之前是否有这个名字的客户
        //String customername = service.getcustomerbyname(customer.getCustomerName());
        String customername = service.getcustomerbynameundertuanding(customer.getCustomerName(),customer.getOrderSn());

        if (customername != null) {
            obj.put("code", 1);
            obj.put("data", "该名称已存在");
            return obj;
        }
        if (customer.getCustomerName() == null || "".equals(customer.getCustomerName())) {
            obj.put("code", 1);
            obj.put("data", "未输入名字，请输入");
            return obj;
        }
        WordUtils util = new WordUtils();
        String wenzi = util.getUpperCase(customer.getCustomerName(), false);
        wenzi=wenzi+System.currentTimeMillis();
        customer.setCustomerCode(wenzi);

        int count = service.addcustomer(customer);
        if (count > 0) {
            int kehuid = service.getidbykehubianhao(wenzi);
            obj.put("code", 0);
            obj.put("data", "新增成功");
            obj.put("msg", kehuid);
        } else {
            obj.put("code", 1);
            obj.put("data", "新增失败");
        }
        return obj;
    }


    /*
    花名册下的新增客户
     */
    @PostMapping("/hmcaddcustomer")
    public JSONObject hmcaddcustomer(
            @RequestBody Customer customer
    ) {
        JSONObject obj = new JSONObject();
        String userid = service.getUseridbytoken(customer.getToken());
        if (userid == null) {
            obj.put("code", 1);
            obj.put("data", "token失效或存户不存在");
            return obj;
        }
        //查询之前是否有这个名字的客户
//        String customername = service.getcustomerbyname(customer.getCustomerName());
//        if (customername != null) {
//            obj.put("code", 1);
//            obj.put("data", "该客户名已存在");
//            return obj;
//        }


        if (customer.getCustomerName() == null || "".equals(customer.getCustomerName())) {
            obj.put("code", 1);
            obj.put("data", "未输入名字，请输入");
            return obj;
        }

        if (customer.getCustomerCode() == null || "".equals(customer.getCustomerCode())) {
            obj.put("code", 1);
            obj.put("data", "未输入工号，请输入");
            return obj;
        }

        //查询这个父级ID下面之前是否有这个编号的客户
        String customercode=customerMapper.getcustomercodebycode(customer.getCustomerCode(),customer.getParentId());
        if(customercode != null) {
            obj.put("code", 1);
            obj.put("data", "该客户编号已存在请修改");
            return obj;
        }

        int count = service.hmcaddcustomer(customer);
        if (count > 0) {
            obj.put("code", 0);
            obj.put("data", "新增成功");
        } else {
            obj.put("code", 1);
            obj.put("data", "新增失败");
        }
        return obj;
    }


    /*
    花名册下的编辑员工前的查询员工详情
     */
    @GetMapping("/gethmccustdetail")
    public JSONObject gethmccustdetail(
            @RequestParam(name = "id") int id
    ) {
        JSONObject obj = new JSONObject();


        Customer customer = service.gethmccustdetail(id);
        if (customer == null) {
            obj.put("code", 1);
            obj.put("msg", "没有此客户");
        } else {
            obj.put("code", 0);
            obj.put("data", customer);
        }
        return obj;
    }


    /*
    花名册下的员工编辑
     */
    @PostMapping("/hmcupdatecustomer")
    public JSONObject hmcupdatecustomer(
            @RequestBody Customer customer
    ) {
        JSONObject obj = new JSONObject();

        if (customer.getCustomerName() == null || "".equals(customer.getCustomerName())) {
            obj.put("code", 1);
            obj.put("data", "未输入名字，请输入");
            return obj;
        }
        int count = service.hmcupdatecustomer(customer);
        if (count > 0) {
            obj.put("code", 0);
            obj.put("data", "编辑成功");
        } else {
            obj.put("code", 1);
            obj.put("data", "编辑失败");
        }
        return obj;
    }


    /*
    新增员工
     */
    @PostMapping("/addstaff")
    public JSONObject addstaff(
            @RequestBody User user

    ) {
        JSONObject obj = new JSONObject();
        String userid = service.getUseridbytoken(user.getToken());
        if (userid == null) {
            obj.put("code", 1);
            obj.put("data", "token失效或存户不存在");
            return obj;
        }
        //判断前端传来的数据是否完整
        if (user.getRealName() == null || "".equals(user.getRealName())
        ) {
            obj.put("code", 1);
            obj.put("data", "姓名未输入，请重新填写");
            return obj;
        }
        if (user.getPhone() == null || "".equals(user.getPhone())
                ) {
            obj.put("code", 1);
            obj.put("data", "手机号未填写，请重新填写");
            return obj;
        }
        //判断员工工号是否重复
        String staffNumber=service.getstaffNumber(user.getCompanyOrgcode(),user.getStaffNumber());
        if (staffNumber != null) {
            obj.put("code", 1);
            obj.put("data", "员工工号已存在");
            return obj;
        }
        try {
            //判断工号是否已经存在
//            String workname = service.queryworknamebyworkname(user.getWorkNumber());
//            if (workname != null) {
//                obj.put("code", 1);
//                obj.put("data", "工号已存在");
//                return obj;
//            }
            //判断登录账号是否已经存在
//            String username = service.queryusername(user.getUserName());
//            if (username != null) {
//                obj.put("code", 1);
//                obj.put("data", "登录账号已存在");
//                return obj;
//            }
            //判断手机号是否已经存在是否已经存在
            String oldphone = service.queryoldphone(user.getPhone());
            if (oldphone != null) {
                obj.put("code", 1);
                obj.put("data", "该手机号已存在，请重新输入");
                return obj;
            }
            //先随机生成一个工号
            Long startTs = System.currentTimeMillis(); // 当前时间戳
//            //8位随机数
            int random = new Random().nextInt(1000000) + 1;//为变量赋随机值1-99999999;
            String worknumber = random + (startTs.toString());
            user.setWorkNumber(worknumber);
            user.setPassWord(String.valueOf(System.currentTimeMillis()));
            //设置密码眼
            String salt = oConvertUtils.randomGen(8);
            user.setSalt(salt);
            String passwordEncode = PasswordUtil.encrypt(user.getWorkNumber(), user.getPassWord(), salt);
//            //设置密码
            user.setPassWord(passwordEncode);
            //设置token
          //  String token2 = JwtUtil.sign(user.getUserName(), user.getPassWord());
            //先把时间戳当token用吧
            user.setToken(startTs.toString());
            user.setCreateTime(new Date());
            user.setMonthLines("0");
            user.setUserName(user.getPhone());
            int count = service.addstaff(user);

            if(user.getIdentity()==0){//普通员工
                //通过公司编码去查询上级的权限编码---查询的是上级的编号
                String upquanxian=service.getupquanxian(user.getCompanyOrgcode());
                //通过账号查询这个user的id
                int userId = backCustService.getuserIdbyusexrname(user.getUserName());
                //获取汉子的首字母
//                WordUtils word = new WordUtils();
//                String zimuname = word.getUpperCase(user.getRealName(),false);
                Long startTscode = System.currentTimeMillis(); // 当前时间戳
                //权限操作，加一个权限编码
                String newcode = upquanxian+"D"+startTscode;
                backCustService.updateOrgCode(userId,newcode);
            }else if(user.getIdentity()==2){//管理员
                //通过公司编码去查询上级的权限编码---查询的是上级的编号
                String upquanxian=service.getupquanxian(user.getCompanyOrgcode());
                int userId = backCustService.getuserIdbyusexrname(user.getUserName());
                //权限操作，加一个权限编码
                service.updatedianzhangOrgCode(userId,upquanxian);
            }
            if (count > 0) {
                obj.put("code", 0);
                obj.put("data", "新增成功");
            } else {
                obj.put("code", 1);
                obj.put("data", "新增失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }

    /*
    删除员工
     */
    @GetMapping("/deletestaff")
    public JSONObject deletestaff(
            int id,
            @RequestParam(name = "token") String token
    ) {
        JSONObject obj = new JSONObject();
        String userid = service.getUseridbytoken(token);
        if (userid == null) {
            obj.put("code", 1);
            obj.put("data", "token失效或存户不存在");
            return obj;
        }
        try {

            //如果该员工是公司账号，则不可以删除
            String conpanycunzai=service.getconpanycunzaibyuserId(id);
            if(conpanycunzai!=null){
                obj.put("code", 1);
                obj.put("data", "不能删除初始账号");
                return obj;
            }

            int count = service.deletestaff(id);
            if (count > 0) {
                obj.put("code", 0);
                obj.put("data", "删除成功");
            } else {
                obj.put("code", 1);
                obj.put("data", "删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }


    /*
    新增标识
     */
    @GetMapping("/addidentification")
    public JSONObject addidentification(
            @RequestParam(name = "token") String token,
            @RequestParam(name = "identification") String identification
    ) {

        JSONObject obj = new JSONObject();
        //此处判断前端传来的值是否为空
        if (identification == null || "".equals(identification)) {
            obj.put("code", 1);
            obj.put("data", "未填写正确的品牌标，请重新填写");
            return obj;
        }

        //通过用户token获取用户id
        String userid = service.getUseridbytoken(token);
        if (userid == null) {
            obj.put("code", 1);
            obj.put("data", "token失效或存户不存在");
            return obj;
        }

        //通过token查询旧的标识
        int count = 0;
        String oldidentification = service.getoldidentification(token);
        if (oldidentification == null) {
            count = service.updateidentification(identification, token);
        } else {
            String newidentification = oldidentification + "," + identification;
            count = service.updateidentification(newidentification, token);
        }
        if (count > 0) {
            obj.put("code", 0);
            obj.put("data", "新增成功");
        } else {
            obj.put("code", 1);
            obj.put("data", "新增失败");
        }
        return obj;
    }

    /*
    查询标识
     */
    @GetMapping("/getidentification")
    public JSONObject getidentification(
            @RequestParam(name = "token") String token) {
        JSONObject obj = new JSONObject();
        //通过用户token获取用户id
        String userid = service.getUseridbytoken(token);
        if (userid == null) {
            obj.put("code", 1);
            obj.put("data", "token失效或存户不存在");
            return obj;
        }
        //通过token查询到公司的id先
        int companyId = service.queryCompanyId(token);
        List<Map<String, Object>> ppbmap;
        ppbmap = service.getppbList(companyId);
        for (int i = 0; i < ppbmap.size(); i++) {

            ppbmap.get(i).put("create_time", ppbmap.get(i).get("create_time").toString().substring(0, 19));

        }
        obj.put("code", 0);
        obj.put("data", ppbmap);
        return obj;
    }


    /*
    通过门店id查询门店信息
     */
    @GetMapping("/getstorebyid")
    public JSONObject getstorebyid(
            @RequestParam(name = "storeId") int storeId
    ) {
        JSONObject obj = new JSONObject();
        store storedetail = service.getstorebyid(storeId);
        obj.put("code", 0);
        obj.put("data", storedetail);
        return obj;
    }


    /*
    编辑门店
     */
    @PostMapping("/updatestore")
    public JSONObject updatestore(
            @RequestBody store store
    ) {
        JSONObject obj = new JSONObject();
        //判断前端传来的信息是否完整
        if (store.getStoreName() == null || "".equals(store.getStoreName())) {
            obj.put("code", 1);
            obj.put("data", "门店名未输入，请输入后再提交");
            return obj;
        }

        String oldStorenamecunzai=service.getoldStorenamecunzai(store.getStoreName());
        if(oldStorenamecunzai!=null&&!oldStorenamecunzai.equals(store.getStoreCode())){
            obj.put("code", 1);
            obj.put("data", "门店名已被使用，请修改");
            return obj;
        }
        try {
            int count = service.updatestore(store);
            if (count > 0) {
                obj.put("code", 0);
                obj.put("data", "修改成功");
            } else {
                obj.put("code", 1);
                obj.put("data", "修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }


    /*
    编辑员工
     */
    @PostMapping("/updatestaff")
    public JSONObject updatestaff(
            @RequestBody User user
    ) {
        JSONObject obj = new JSONObject();
        //判断前端传来的数据是否完整
        if (user.getRealName() == null || "".equals(user.getRealName())) {
            obj.put("code", 1);
            obj.put("data", "姓名未输入，请重新填写");
            return obj;
        }
        if (user.getPhone() == null || "".equals(user.getPhone())) {
            obj.put("code", 1);
            obj.put("data", "手机号未填写，请重新填写");
            return obj;
        }

        //判断员工工号是否重复
//        String staffNumber=service.getstaffNumber(user.getCompanyOrgcode(),user.getStaffNumber());
//        if (staffNumber != null&&user.getStaffNumber().equals("staffNumber")) {
//            obj.put("code", 1);
//            obj.put("data", "员工工号已存在");
//            return obj;
//        }

//        MlccUserInfo userInfo2 = userservice.queryUserinfoByusername(user.getUserName(), 1);
//        if (userInfo2 != null) {
//            obj.put("code", 3);
//            obj.put("data", "该用户账号已存在");
//            return obj;
//        }

        //设置密码眼
        //String salt = oConvertUtils.randomGen(8);
        //user.setSalt(salt);
        //String passwordEncode = PasswordUtil.encrypt(user.getWorkNumber(), user.getPassWord(), salt);
        //设置密码
        //user.setPassWord(passwordEncode);

        //通过用户id去公司表查询公司名称
        String companyName=service.getcompanyNamebyuserId(user.getId());
        if(companyName!=null){
            user.setIdentity(2);
        }else {
            if(user.getIdentity()==0){//普通员工
                //通过公司编码去查询上级的权限编码---查询的是上级的编号
                String upquanxian=service.getupquanxian(user.getCompanyOrgcode());
                //通过账号查询这个user的id
                int userId = user.getId();
                Long startTscode = System.currentTimeMillis(); // 当前时间戳
                //权限操作，加一个权限编码
                String newcode = upquanxian+"D"+startTscode;
                //权限操作，加一个权限编码
                backCustService.updateOrgCode(userId,newcode);
                user.setIdentity(0);
            }else if(user.getIdentity()==2){//管理员
                //通过公司编码去查询上级的权限编码---查询的是上级的编号
                String upquanxian=service.getupquanxian(user.getCompanyOrgcode());
                int userId = user.getId();
                //权限操作，加一个权限编码
                service.updatedianzhangOrgCode(userId,upquanxian);
                user.setIdentity(2);
            }
        }
        int count = service.updatestaff(user);
        if (count > 0) {
            obj.put("code", 0);
            obj.put("data", "修改成功");
        } else {
            obj.put("code", 1);
            obj.put("data", "修改失败");
        }
        return obj;
    }

    /*
    根据人员id查询人员信息
     */
    @GetMapping("/getstaffbyid")
    public JSONObject getstaffbyid(
            @RequestParam(name = "id") int id
    ) {
        JSONObject obj = new JSONObject();
        User staff = service.getstaffbyid(id);
        obj.put("code", 0);
        obj.put("data", staff);
        return obj;
    }

    /*
    设置中通过token查询到一些用户信息
     */
    @GetMapping("/getcompanysetbytoken")
    public JSONObject getcompanysetbytoken(
            @RequestParam(name = "token") String token
    ) {

        JSONObject obj = new JSONObject();

        User user = service.getcompanysetbytoken(token);

        if (user == null) {
            obj.put("code", 1);
            obj.put("data", "没有信息");
        } else {
            obj.put("code", 0);
            obj.put("data", user);
        }

        return obj;
    }

    /*
    我的公司中的设置中的数据修改
     */
    @PostMapping("/updatecompanysetbytoken")
    public JSONObject updatecompanysetbytoken(
            @RequestBody User user
    ) {
        JSONObject obj = new JSONObject();
        int count = service.updatecompanysetbytoken(user);
        if (count > 0) {
            obj.put("code", 0);
            obj.put("data", "修改成功");
        } else {
            obj.put("code", 1);
            obj.put("data", "修改失败");
        }
        return obj;
    }


    /*
    通过token获取公司id
     */
    @GetMapping("/queryCompanyId")
    public JSONObject queryCompanyId(
            @RequestParam(name = "token") String token) {
        JSONObject obj = new JSONObject();
        MlccUserInfo userInfo = userservice.queryUserinfoByusername(token, 3);
        if (userInfo == null) {
            obj.put("code", 4);
            obj.put("msg", "token已失效，请重新登录");
            return obj;
        }
        int companyId = service.queryCompanyId(token);
        obj.put("data", companyId);
        obj.put("companyCode", userInfo.getCompanyOrgcode());
        obj.put("code", 0);
        return obj;
    }

    /*
    通过token获取公司的code
     */
    @GetMapping("/queryCompanyCode")
    public JSONObject queryCompanyCode(
            @RequestParam(name = "token") String token
    ) {
        JSONObject obj = new JSONObject();
        String companyCode = service.queryCompanyCode(token);
        obj.put("data", companyCode);
        obj.put("code", 0);
        return obj;
    }


    /*
    删除品牌标
     */
    @GetMapping("/deleteppb")
    public JSONObject deleteppb(
            @RequestParam(name = "id") int id) {
        JSONObject obj = new JSONObject();

        int count = service.deleteppb(id);
        if (count > 0) {
            obj.put("code", 0);
            obj.put("data", "删除成功");
        } else {
            obj.put("code", 1);
            obj.put("data", "删除失败");
        }
        return obj;
    }

    /*
    编辑修改品牌标志
     */
    @GetMapping("/updateppb")
    public JSONObject updateppb(
            @RequestParam(name = "id") int id,
            @RequestParam(name = "ppbname") String ppbname,
            @RequestParam(name = "companyId") String companyId
    ) {


        JSONObject obj = new JSONObject();
        if (ppbname == null || "".equals(ppbname)) {
            obj.put("data", "您未输入品牌标");
            obj.put("code", 1);
            return obj;
        }


        //判断该公司下该品牌标是否已经存在
        String oldppb=service.getoldppb(ppbname,companyId);
        if(oldppb!=null){

            int cunzaiid=service.getoldppbid(ppbname,companyId);
            if(cunzaiid!=id){
                obj.put("code", 1);
                obj.put("data", "该品牌标已存在，请修改");
                return obj;
            }

        }

        int count = service.updateppb(id, ppbname);
        if (count > 0) {
            obj.put("code", 0);
            obj.put("data", "修改成功");
        } else {
            obj.put("code", 0);
            obj.put("data", "修改失败");
        }
        return obj;
    }

    /*
     新增品牌标
     */
    @GetMapping("/addppb")
    public JSONObject addppb(
            @RequestParam(name = "companyId") String companyId,
            @RequestParam(name = "ppbname") String ppbname
    ) {
        JSONObject obj = new JSONObject();

         //判断该公司下该品牌标是否已经存在
        String oldppb=service.getoldppb(ppbname,companyId);
        if(oldppb!=null){
            obj.put("code", 1);
            obj.put("data", "该品牌标已存在，请修改");
            return obj;
        }
        int count = service.addppb(companyId, ppbname);
        if (count > 0) {
            obj.put("code", 0);
            obj.put("data", "新增成功");
        } else {
            obj.put("code", 1);
            obj.put("data", "新增失败");
        }
        return obj;
    }


}

